package com.dg.gaming.client.gui {
    import com.dg.gaming.api.common.Player;
    import com.dg.gaming.api.common.PlayerList;
    import com.dg.gaming.api.common.Table;
    import com.dg.gaming.api.common.util.Color;
    import com.dg.gaming.api.common.util.GameProperties;
    import com.dg.gaming.client.TableConnectionThread;

    import flash.display.DisplayObject;

    import mx.containers.Canvas;
    import mx.containers.Grid;
    import mx.containers.GridItem;
    import mx.containers.GridRow;
    import mx.controls.Label;

    public class PlayerComponent extends Canvas {
        // Sizes
        private static const SIZE:int = 20;



        // Fields
        private var index:int;

        private var conn:TableConnectionThread;

        private var leftAlign:Boolean;

        private var drawArrow:Boolean;

        private var table:Table; // link to table object

        // GUI items
        private var playerSquare:ColouredSquare;

        private var userLabel:Label;

        private var turnArrow:TurnArrow;

        public function PlayerComponent(conn:TableConnectionThread, index:int, leftAlign:Boolean = true, drawArrow:Boolean = true) {
            super();
            this.index = index;
            this.conn = conn;
            if (conn != null)
                this.table = conn.getTable();
            this.leftAlign = leftAlign;
            this.drawArrow = drawArrow;
            // Create GUI items
            createGUI();
            // Add observers
            if (conn != null) {
                table.addObserver(update);
                // Add observers
                table.getPlayerList().addObserver(update);
                if (table.getNumOfPlayers() <= index) {
                    playerSquare.setVisible(false);
                }
            }
            refresh();
        }

        public function update(evnt:Observer):void {
            refresh();
        }


        /**
         * Refresh.
         */
        private function refresh():void {
            if (table == null)
                return;
            var players:PlayerList = table.getPlayerList();
            var player:Player = players.getPlayerBySeat(index);

            // Update username
            var username:String = "nop layer";
            if (player != null) {
                username = player.toUserName();
            }
            userLabel.text = (username + "");

            // Update turn arrow visiblity
            if (player != null) {
                turnArrow.setVisible(players.isCurrentPlayer(player));
            } else {
                turnArrow.setVisible(false);
            }
        }

        private function addRows(grid:Grid, compArray:Array):void {
            var len:int = compArray.length;
            for (var x:int = 0; x < len; x++) {
                var gridRow:GridRow = new GridRow();
                grid.addChild(gridRow);
                gridRow.percentHeight = 100;
                gridRow.percentWidth = 100;
                var item:GridItem = new GridItem();
                gridRow.addChild(item);
                item.percentHeight = 100;
                item.percentWidth = 100;
                item.addChild(DisplayObject(compArray[x]));
            }
        }

        /**
         * Create GUI.
         */
        private function createGUI():void {
            this.playerSquare = new ColouredSquare(GameProperties.getPlayerColour(index), SIZE, SIZE, true);
            this.playerSquare.useHandCursor = true;
            userLabel = new Label();
            userLabel.text = "" + index;
            // FIXME move these to "game.properties" at some stage?
            this.turnArrow = new TurnArrow(Color.red, Color.black, leftAlign);
            var grid:Grid = new Grid();
            this.addChild(grid);
            grid.setStyle("horizontalGap", 1);
            grid.setStyle("verticalGap", 1);
//            grid.setStyle("backgroundColor", 0);
            if (leftAlign) {
                if (drawArrow) {
                    addRows(grid, [ playerSquare, userLabel, turnArrow ]);
//                setLayout(new double[][] { {pref, 5, pref, 5, 15}, {pref}
//                });
//                add(playerSquare, "0,0,l,b");
//                add(userLabel, "2,0,l,b");
//                add(turnArrow, "4,0,l,c");
                } else {
                    addRows(grid, [ playerSquare, userLabel ]);
//                setLayout(new double[][] { {pref, 5, pref}, {pref}
//                });
//                add(playerSquare, "0,0,l,b");
//                add(userLabel, "2,0,l,b");
                }
            } else {
                if (drawArrow) {
                    addRows(grid, [ playerSquare, userLabel, turnArrow ]);
//                setLayout(new double[][] { {15, 5, pref, 5, pref}, {pref}
//                });
//                add(playerSquare, "4,0,l,b");
//                add(userLabel, "2,0,l,b");
//                add(turnArrow, "0,0,l,c");
                } else {
                    addRows(grid, [ playerSquare, userLabel ]);
//                setLayout(new double[][] { {pref, 5, pref}, {pref}
//                });
//                add(playerSquare, "2,0,l,b");
//                add(userLabel, "0,0,l,b");
                }
            }

        }
    }
}